package com.car.carcontrol;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Rect;
import android.media.tv.TvView;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;


import com.car.carcontrol.base.BaseActivity;
import com.car.carcontrol.base.BaseApplication;
import com.car.carcontrol.util.IntentUtils;
import com.car.carcontrol.util.SharedPreferencesUrls;
import com.car.carcontrol.util.Util_Rotate3DAnimation;
import com.car.carcontrol.util.Util_Rotate3DAnimation2;
import com.car.carcontrol.util.Utils;
import com.dalong.library.listener.OnItemSelectedListener;
import com.dalong.library.listener.OnLoopViewTouchListener;
import com.dalong.library.view.LoopRotarySwitchView;
import com.easy.carpanel.uart.uartJni;
import com.google.gson.Gson;
import com.monkey.miclockview.MiClockView;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

//首页、启动页
public class MainActivity extends BaseActivity {

    private FrameLayout v;  //整个界面的Layout
    private Activity activity;  //用于界面跳转

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");  //时间格式 时:分
    private SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy/MM/dd");    //日期格式 年/月/日

    private long loopTime = 60 * 1000;  //时间显示间隔   默认1分钟
    private int cn = 10;     //屏保显示间隔   默认10分钟

    private FrameLayout framelayout;    //旋转时当前界面的Layout
    private FrameLayout framelayout2;    //旋转时目的界面的Layout

    float dx;   //用于界面旋转的触摸横向移动量
    float x;    //导航栏按下时的横向坐标

    boolean first = true;   //是否第一次按下
    boolean isEnd = false;  //手指是否抬起

    private float degrees;  //当前界面旋转角度
    private float degrees2;  //目的界面旋转角度

    int pos;    //item位置id

    int id = 1; //旋转起始界面所在framelayout的id
    int id2 = 2; //旋转目的界面所在framelayout的id

    //获取屏幕分辨率
    int width = BaseApplication.width;  //宽
    int height = BaseApplication.height;    //高
    float density = BaseApplication.density;    //密度

    boolean left_first = true;  //是否刚开始旋转
    public static boolean isStop = false;   //旋转是否停止
    public static int curPos;   //当前界面id

    Handler handler = new Handler();

    private TextView tv_temp;   //温度控件
    private TextView tv_time;   //时间控件
    private TextView tv_date;   //日期控件

    private MiClockView miClockView;    //屏保界面

    private Fragment fromFrag;  //起始界面类
    private Fragment toFrag;  //目的界面类

    private int toPos;  //目的界面id
    private int toPos2;  //目的界面id2

    Fragment[] frags;   //界面类数组

    public static String[] names;   //导航栏item名字数组

    Object[] allParam;  //控件参数数组

    byte[] bytes_temp;  //读取温度串口指令

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        v = new FrameLayout(this);
        setContentView(v);

//        TvView tv = new TvView(this);
//        tv.ru

        context = this;
        activity = this;

        Log.e("ma===onCreate", width+"==="+height+"==="+density);

//        try {
////            String s0 = "[\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":340,\\\\\\\"left\\\\\\\":0,\\\\\\\"right\\\\\\\":984,\\\\\\\"top\\\\\\\":0}\\\",2131165295,0,false,\\\"[6,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":95,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":10}\\\",2131165449,2131165450,true,\\\"[6,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":427,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":10}\\\",2131165455,2131165456,true,\\\"[9,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":760,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":10}\\\",2131165390,2131165391,true,\\\"[16,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":95,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":180}\\\",2131165447,2131165448,true,\\\"[5,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":427,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":180}\\\",2131165449,2131165450,true,\\\"[6,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":760,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":180}\\\",2131165392,2131165393,true,\\\"[17,1,2]\\\"]\"]";
//            String s0 = "[\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":340,\\\\\\\"left\\\\\\\":0,\\\\\\\"right\\\\\\\":984,\\\\\\\"top\\\\\\\":0}\\\",2131165295,0,false,\\\"[6,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":95,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":10}\\\",2131165449,2131165450,true,\\\"[6,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":427,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":10}\\\",2131165455,2131165456,true,\\\"[9,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":760,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":10}\\\",2131165390,2131165391,true,\\\"[16,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":95,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":180}\\\",2131165447,2131165448,true,\\\"[5,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":427,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":180}\\\",2131165449,2131165450,true,\\\"[6,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":760,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":180}\\\",2131165392,2131165393,true,\\\"[17,1,2]\\\"]\"]"
//                    +"[\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":340,\\\\\\\"left\\\\\\\":0,\\\\\\\"right\\\\\\\":984,\\\\\\\"top\\\\\\\":0}\\\",2131457568295,0,false,\\\"[6,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":95,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":10}\\\",2131165449,2131165450,true,\\\"[6,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":427,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":10}\\\",2131165455,2131165456,true,\\\"[9,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":760,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":10}\\\",2131165390,2131165391,true,\\\"[16,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":95,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":180}\\\",2131165447,2131165448,true,\\\"[5,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":427,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":180}\\\",2131165449,2131165450,true,\\\"[6,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":760,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":180}\\\",2131165392,2131165393,true,\\\"[17,1,2]\\\"]\"]"
//            +"[\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":340,\\\\\\\"left\\\\\\\":0,\\\\\\\"right\\\\\\\":984,\\\\\\\"top\\\\\\\":0}\\\",2131un8295,0,false,\\\"[6,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":95,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":10}\\\",2131165449,2131165450,true,\\\"[6,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":427,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":10}\\\",2131165455,2131165456,true,\\\"[9,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":760,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":10}\\\",2131165390,2131165391,true,\\\"[16,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":95,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":180}\\\",2131165447,2131165448,true,\\\"[5,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":427,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":180}\\\",2131165449,2131165450,true,\\\"[6,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":760,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":180}\\\",2131165392,2131165393,true,\\\"[17,1,2]\\\"]\"]";
//
//            String s = Utils.compress(s0);
////            String s = bytesToString(compress("[\"[\\\"iv\\\",\\\"{\\\\\\\"bottom"));
////            String s = bytesToString(compress("中国Chinaeaerfupuiferweff"));
////            String s = Utils.bytesToString(Utils.compress(s0)).replace(" ", "");
////            String s = compress(" [\"[\\\"iv\\\",\\\"{\\\\\\\"bottom");
//
////            50 4B 03 04 14 00 08 08 08 00 9C 32 86 50 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 30 7B B2 63 ED D3 D9 7B 9D 33 32 F3 12 01 50 4B 07 08 EA DC 06 93 0D 00 00 00 0B 00 00 00
////            50 4B 03 04 14 00 08 08 08 00 FC 32 86 50 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 30 7B B2 63 ED D3 D9 7B 9D 33 32 F3 12 9F 20 98 00 50 4B 07 08 96 82 A4 C1 10 00 00 00 16 00 00 00
////            50 4B 03 04 14 00 08 08 08 00 22 33 86 50 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 30 7B B2 63 ED D3 D9 7B 9D 33 32 F3 12 53 13 53 8B D2 4A 0B 4A 33 D3 52 8B CA 53 D3 D2 00 50 4B 07 08 33 2C 3D 14 1D 00 00 00 1B 00 00 00
//
////            504B03041400080808003C3686500000000000000000000000000100000030ED93CB0EC2201045FF85358B0E303CFC95D28526AD36A9D6287563FC77FB48405C343EC28EDD9D2199935C724A525AD2DE2CA196DCADB564D73BD71FA7B4E1A2A053E8EAC6CD8B65BCB4FBC3321B2DE68DEBCFCBFB63BCC3800348640669419B6D77ADC7D3A5A44059654945E80A1190C5C4F1488C04AE2224BC3285303E6241DD65F8872D98FA0A8E18A2F470F31B5CC9F7B257E1DC1421828743B2DA75DCBB0A517B3A26EB5DA7FBF50F8AD771F32C441E9A571E5F95491C13A850EAAC59D62C6B9650B3E1941DCB8E65C716C79E504B07085CFB1733CF0000002F0A0000
//
////            s=""
//
////            String s1 = new String(s.getBytes("gbk"),"ISO-8859-1");
//            String s_iso88591 = new String("中".getBytes("UTF-8"),"UTF-8");
//            String s1 = new String(s.getBytes("ISO8859-1"),"ISO8859-1");
////            String s1 = new String(s.getBytes("ISO-8859-1"),"utf-8");
////
////            Gson gson = new Gson();
//            Log.e("ma===onCreate1",  "==="+s_iso88591);
//
////            String s2 = new String(s1.getBytes("utf-8"),"ISO-8859-1");
////            String s2 = new String(s1.getBytes("ISO-8859-1"),"gbk");
//
////            Log.e("ma===onCreate1", width+"==="+height+"==="+density+"==="+s0.length()+"==="+s.length()+"==="+s+"==="+Utils.hexStringToByteArray(s)+"==="+Utils.decompress(Utils.hexStringToByteArray(s)).length());
//            Log.e("ma===onCreate1_2",  s1+"==="+Utils.decompress(s));
////            Log.e("ma===onCreate1", s.length()+"==="+Utils.decompress(s));
////            Log.e("ma===onCreate1", width+"==="+height+"==="+density+"==="+s0.length()+"==="+s.length()+"==="+s+"==="+Utils.decompress(s));
//
////            Log.e("ma===onCreate1", width+"==="+height+"==="+density+"==="+s+"==="+decompress(hexStringToByteArray(s)));
//        } catch (Exception e) {
//            Log.e("ma===onCreate==e", e+"===");
//            e.printStackTrace();
//        }
//
//        SharedPreferences sp = BaseApplication.getInstance().getSharedPreferences(BaseApplication.getInstance().getPackageName(), Context.MODE_PRIVATE);
//
//        Log.e("ma===onCreate2", "==="+ sp.getString("allParam7", ""));
//
//        Gson gson = new Gson();
////        String s = gson.toJson(Config.mapAll);
////        String s = gson.toJson(sp);
//
////        {"allParam8":"[\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":765,\\\\\\\"left\\\\\\\":0,\\\\\\\"right\\\\\\\":1665,\\\\\\\"top\\\\\\\":0}\\\",2131165295,0,false,\\\"[8,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":342,\\\\\\\"left\\\\\\\":161,\\\\\\\"right\\\\\\\":233,\\\\\\\"top\\\\\\\":225}\\\",2131165453,2131165454,true,\\\"[8,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":342,\\\\\\\"left\\\\\\\":724,\\\\\\\"right\\\\\\\":233,\\\\\\\"top\\\\\\\":225}\\\",2131165390,2131165391,true,\\\"[16,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":342,\\\\\\\"left\\\\\\\":1288,\\\\\\\"right\\\\\\\":233,\\\\\\\"top\\\\\\\":225}\\\",2131165392,2131165393,true,\\\"[17,1,2]\\\"]\"]","names":"","allParam5":"","allParam4":"","allParam7":"","allParam6":"","allParam1":"","allParam3":"","allParam2":""}
//
////        String s = "{\"allParam8\":\"[\\\"[\\\\\\\"iv\\\\\\\",\\\\\\\"{\\\\\\\\\\\\\\\"bottom\\\\\\\\\\\\\\\":765,\\\\\\\\\\\\\\\"left\\\\\\\\\\\\\\\":0,\\\\\\\\\\\\\\\"right\\\\\\\\\\\\\\\":1665,\\\\\\\\\\\\\\\"top\\\\\\\\\\\\\\\":0}\\\\\\\",2131165295,0,false,\\\\\\\"[8,1,2]\\\\\\\"]\\\",\\\"[\\\\\\\"iv\\\\\\\",\\\\\\\"{\\\\\\\\\\\\\\\"bottom\\\\\\\\\\\\\\\":342,\\\\\\\\\\\\\\\"left\\\\\\\\\\\\\\\":161,\\\\\\\\\\\\\\\"right\\\\\\\\\\\\\\\":233,\\\\\\\\\\\\\\\"top\\\\\\\\\\\\\\\":225}\\\\\\\",2131165453,2131165454,true,\\\\\\\"[8,1,2]\\\\\\\"]\\\",\\\"[\\\\\\\"iv\\\\\\\",\\\\\\\"{\\\\\\\\\\\\\\\"bottom\\\\\\\\\\\\\\\":342,\\\\\\\\\\\\\\\"left\\\\\\\\\\\\\\\":724,\\\\\\\\\\\\\\\"right\\\\\\\\\\\\\\\":233,\\\\\\\\\\\\\\\"top\\\\\\\\\\\\\\\":225}\\\\\\\",2131165390,2131165391,true,\\\\\\\"[16,1,2]\\\\\\\"]\\\",\\\"[\\\\\\\"iv\\\\\\\",\\\\\\\"{\\\\\\\\\\\\\\\"bottom\\\\\\\\\\\\\\\":342,\\\\\\\\\\\\\\\"left\\\\\\\\\\\\\\\":1288,\\\\\\\\\\\\\\\"right\\\\\\\\\\\\\\\":233,\\\\\\\\\\\\\\\"top\\\\\\\\\\\\\\\":225}\\\\\\\",2131165392,2131165393,true,\\\\\\\"[17,1,2]\\\\\\\"]\\\"]\",\"names\":\"\",\"allParam5\":\"\",\"allParam4\":\"\",\"allParam7\":\"\",\"allParam6\":\"\",\"allParam1\":\"\",\"allParam3\":\"\",\"allParam2\":\"\"}";
//        String s = "{\"allParam8\":\"\",\"names\":[\"灯光\",\"码表\",\"净化器\",\"天窗\",\"多媒体\",\"空调\",\"信息\",\"电器\"],\"allParam5\":\"\",\"allParam4\":\"\",\"allParam7\":\"[\\\"[\\\\\\\"iv\\\\\\\",\\\\\\\"{\\\\\\\\\\\\\\\"bottom\\\\\\\\\\\\\\\":765,\\\\\\\\\\\\\\\"left\\\\\\\\\\\\\\\":0,\\\\\\\\\\\\\\\"right\\\\\\\\\\\\\\\":1665,\\\\\\\\\\\\\\\"top\\\\\\\\\\\\\\\":0}\\\\\\\",2131165295,0,false,\\\\\\\"[16,1,2]\\\\\\\"]\\\",\\\"[\\\\\\\"iv\\\\\\\",\\\\\\\"{\\\\\\\\\\\\\\\"bottom\\\\\\\\\\\\\\\":342,\\\\\\\\\\\\\\\"left\\\\\\\\\\\\\\\":724,\\\\\\\\\\\\\\\"right\\\\\\\\\\\\\\\":233,\\\\\\\\\\\\\\\"top\\\\\\\\\\\\\\\":225}\\\\\\\",2131165390,2131165391,true,\\\\\\\"[16,1,2]\\\\\\\"]\\\"]\",\"allParam6\":\"\",\"allParam1\":\"\",\"allParam3\":\"\",\"allParam2\":\"\"}";
//
//
//        HashMap<String, Object> map = gson.fromJson(s, HashMap.class);
//        Log.e("ma===onCreate3", "==="+gson.fromJson(s, HashMap.class));   //{"mFile":{"path":"/data/user/0/com.car.carcontrol/shared_prefs/.xml"}}
////        Log.e("ma===onCreate3", s+"==="+gson.fromJson(s, SharedPreferences.class));
////        Log.e("ma===onCreate3", s+"==="+ gson.fromJson(s, SharedPreferences.class).getString("allParam7", ""));
//
//        for (Map.Entry<String, Object> entry : map.entrySet()) {
////            System.out.println("键值对:" + entry);
////            //获取 键值对的 key
////            System.out.println("key:" + entry.getKey());
////            //获取 键值对的 value
////            System.out.println("value:" + entry.getValue());
//
//            Log.e("ma===onCreate4", entry.getKey()+"==="+entry.getValue());
//
//            if("names".equals(entry.getKey())){
//                SharedPreferencesUrls.getInstance().putObject(entry.getKey(), ((ArrayList)entry.getValue()).toArray(new String[0]));
//            }else{
//                SharedPreferencesUrls.getInstance().putString(entry.getKey(), (String)entry.getValue());
//            }
//
//        }
//
//
//        int resId = getResources().getIdentifier("light1", "drawable" , getPackageName());
//        Log.e("ma===onCreate5", resId+"==="+R.drawable.light1);
//
//        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.light2);
//        String bs = Utils.bitmaptoString(bitmap);
//        Log.e("ma===onCreate6", bs+"==="+Utils.stringtoBitmap(bs));

    }






    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onResume() {
        super.onResume();

        names = (String[])SharedPreferencesUrls.getInstance().getObject("names");   //读取内部存储器：导航转盘的item名字数组

        Log.e("ma===onResume", width+"==="+height+"==="+names);

        if(names==null){    //如果item名字数组为空用默认值，即"灯光", "码表", "净化器", "天窗", "多媒体", "空调"
            names = Config.names;
        }else{
            Log.e("ma===onResume2", width+"==="+height+"==="+names.length+"==="+Config.mapTab.size());
        }

        //根据item名字获取Fragment——每个显示区对应的类
        frags = new Fragment[names.length];
        for (int i = 0; i < names.length; i++) {
            frags[i] = Config.mapTab.get(names[i]);
        }

        Log.e("ma===onResume3", width+"==="+height+"==="+names.length+"==="+frags.length+"==="+frags[0]);


        //界面所有控件的参数object数组，采用Object嵌套object思想，每个子object数组是各种控件，object数组可以放各种类型的参数，有"iv"——String型(控件类型)、Rect——java自带对象(控件大小位置)、R.drawable.bg——int型(控件资源id)、false——boolean型（控件能否点击）、byte[]——数组型（控件点击后发送的串口指令）等
        //iv——图片: 控件类型、控件大小位置、图片资源id、能否点击、点击后发送的指令；
        //tv——文字: 控件类型、控件大小位置、名字、字体大小、字体颜色；
        //lrsv——导航转盘: 控件类型、控件大小位置、item名字数组、字体大小、字体颜色；
        //fl——显示区: 控件类型、控件大小位置、item名字数组、字体大小、字体颜色；
        //mcv——小米时钟屏保界面: 控件类型、控件大小位置、名字、字体大小、界面背景颜色、时钟背景图片资源id、秒针图片资源id、分针图片资源id、时针图片资源id、圆心图片资源id
        allParam = new Object[]{
                new Object[]{"iv", new Rect(0*width/1280, 0*height/480, 1280*width/1280, 480*height/480), R.drawable.bg, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
                new Object[]{"iv", new Rect(0*width/1280, 0*height/480, 246*width/1280, 477*height/480), R.drawable.bg_left, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
                new Object[]{"iv", new Rect(475*width/1280, 85*height/480, 483*width/1280, 30*height/480), R.drawable.bg_top, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},

                new Object[]{"lrsv", new Rect(407*width/1280, 40*height/480, 655*width/1280, 110*height/480), names, 24, Color.WHITE},
                new Object[]{"fl", new Rect(246*width/1280, 140*height/480, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)},

                new Object[]{"tv", new Rect(27*width/1280, 20*height/480, 0, 0), "温度", 18, 0xFF4785E2, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
                new Object[]{"tv", new Rect(1032*width/1280, 19*height/480, 0, 0), "时间", 18, 0xFF4785E2, false},
                new Object[]{"tv", new Rect(1145*width/1280, 28*height/480, 0, 0), "日期", 10, 0xFF4785E2, false},
                new Object[]{"tv", new Rect(1240*width/1280, 19*height/480, 0, 0), "UI定制", 18, Color.GREEN, true},

                new Object[]{"tv", new Rect(64*width/1280, 90*height/480, 0, 0), "投屏", 19, Color.WHITE, true},
                new Object[]{"tv", new Rect(76*width/1280, 150*height/480, 0, 0), "视频", 19, Color.WHITE, true},
                new Object[]{"tv", new Rect(90*width/1280, 210*height/480, 0, 0), "音乐", 19, Color.WHITE, true},
                new Object[]{"tv", new Rect(91*width/1280, 270*height/480, 0, 0), "蓝牙", 19, Color.WHITE, true},
                new Object[]{"tv", new Rect(74*width/1280, 330*height/480, 0, 0), "网络", 19, Color.WHITE, true},
                new Object[]{"tv", new Rect(64*width/1280, 390*height/480, 0, 0), "设置", 19, Color.WHITE, true},

                new Object[]{"mcv", new Rect(0*width/1280, 0*height/480, 1280*width/1280, 480*height/480), "小米时钟", 19, Color.BLACK, R.drawable.clock_bg, R.drawable.needle_second, R.drawable.needle_minute, R.drawable.needle_hour, R.drawable.open},
        };

        initView(); //加载并显示界面

        loop(); //轮循
    }



    //加载并显示界面
    @RequiresApi(api = Build.VERSION_CODES.M)
    private void initView(){
        for (int i=0; i<allParam.length; i++){
            Object[] obj = (Object[])allParam[i];   //获取allParam的每个元素，一个元素即为界面的一个控件
            Rect rect = (Rect)obj[1];   //获取控件的大小位置
            String type = (String)obj[0];   //获取控件的类型，有iv、tv、mcv、lrsv、fl等

            if("tv".equals(type)){  //TextView 加载文字
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);   //FrameLayout.LayoutParams.WRAP_CONTENT：表示包住文字的宽高
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);    //设置文字的文字left、top、right、bottom， 注：right、bottom没用

                String name = (String)obj[2];   //获取文字名字
                int textSize = (int)obj[3];     //获取文字大小
                int textColor = (int)obj[4];     //获取文字颜色
                TextView tv = new TextView(context);
                tv.setText(name);   //设置文字名字
                tv.setTextSize(textSize);   //设置文字大小
                tv.setTextColor(textColor);   //设置文字颜色
                v.addView(tv, params);  //添加并设置文字布局

                if("温度".equals(name)){
                    tv_temp = tv;   //提取出全局变量，用于轮循更新温度
                    bytes_temp = (byte[])obj[6];     //获取读取温度的串口指令
                    tv.setText(""); //设置温度的默认值为空
                }else if("时间".equals(name)){
                    tv_time = tv;   //提取出全局变量，用于轮循更新时间
                    tv.setText(""); //设置时间的默认值为空

                    //点击时间显示屏保
                    tv_time.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            miClockView.setVisibility(View.VISIBLE);
                        }
                    });
                }else if("日期".equals(name)){
                    tv_date = tv;   //提取出全局变量，用于轮循更新日期
                    tv.setText(""); //设置时间的默认值为空

                    //点击日期显示屏保
                    tv_date.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            miClockView.setVisibility(View.VISIBLE);
                            finish();
                        }
                    });
                }

                boolean isCanClick = (boolean)obj[5];

                //判断文字是否可点击，可点击才设置点击和触摸事件，并发送串口指令
                if(isCanClick){

                    //设置文字点击事件
                    tv.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {

                            Log.e("ma===tv", name+"===");

                            Intent intent = new Intent();
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            if("UI定制".equals(name)){
                                IntentUtils.startActivity(activity, SettingActivity.class);
//                                IntentUtils.startActivity(activity, LoginActivity.class);   //跳转到登录界面
                                finish();
                            }else if("投屏".equals(name)){

                                //1.百度地图包名 "com.baidu.BaiduMap";
                                //2.高德地图包名 "com.autonavi.minimap";
                                //3.腾讯地图包名 "com.tencent.map";
                                //4.谷歌地图包名  "com.google.android.apps.maps"
                                //5.高德地图车镜版本（后视镜）包名 "com.autonavi.amapautolite";
                                //6.高德地图车机版本包名  "com.autonavi.amapauto";

                                intent = getPackageManager().getLaunchIntentForPackage("com.autonavi.amapauto");   //跳转到高德地图车机版
                                startActivity(intent);
                            }else if("视频".equals(name)){
//                                Intent intent = new Intent(android.intent.action.VIEW);
                                intent.setAction(Intent.ACTION_VIEW);
//                                intent.setDataAndType(Uri.parse("file:///" + ""), "video/*");
//                                intent.setDataAndType(null, "video/*");
//                                localIntent.setAction(Intent.ACTION_OPEN_DOCUMENT);

//                                intent.setType("video/*");
//                                startActivity(intent);

                                String path = Environment.getExternalStorageDirectory().getPath()+ "/1.mp4";//该路径可以自定义
                                File file = new File(path);
                                Uri uri = Uri.fromFile(file);
                                intent.setDataAndType(uri, "video/*");
                                startActivity(intent);     //跳转到视频界面

//                                Intent intent = new Intent(Intent.ACTION_VIEW);
//                                intent.setDataAndType(Uri.parse(Environment.getExternalStorageDirectory() + "/3.mp4"), "video/mp4");
//                                startActivity(intent);
                            }else if("音乐".equals(name)){
                                intent.setAction(Intent.ACTION_VIEW);

                                String path = Environment.getExternalStorageDirectory().getPath()+ "/a.MP3";//该路径可以自定义
                                File file = new File(path);
                                Uri uri = Uri.fromFile(file);
                                intent.setDataAndType(uri, "audio/MP3");
                                startActivity(intent);     //跳转到音乐界面

//                                Intent it = new Intent(Intent.ACTION_VIEW);
//                                it.setDataAndType(Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/a.MP3"), "audio/MP3");
//                                startActivity(it);


//                                intent.setAction(Intent.ACTION_PICK);
//                                intent.setDataAndType(Uri.EMPTY,"vnd.android.cursor.dir/playlist");
//                                intent.putExtra("withtabs", true); // 显示tab选项卡
//                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//
//                                Intent j =Intent.createChooser(intent, "Choose an application to open with:");
//                                if (j == intent) {
//                                    startActivity(j);
//                                } else {
//                                    intent = new Intent("android.intent.action.MUSIC_PLAYER");
//                                    startActivity(intent);
//                                }
                            }else if("蓝牙".equals(name)){
                                intent.setAction(Settings.ACTION_BLUETOOTH_SETTINGS);
                                startActivity(intent);     //跳转到蓝牙界面
                            }else if("网络".equals(name)){
//                                localIntent.setAction(Settings.ACTION_WIFI_SETTINGS);
                                intent.setAction(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
                                startActivity(intent);     //跳转到网络界面
                            }else if("设置".equals(name)){
                                intent.setAction(Settings.ACTION_SETTINGS);
//                                localIntent.setData(Uri.fromParts("package", getPackageName(), null));
                                startActivity(intent);     //跳转到设置界面
                            }

                        }
                    });
                }

            }else if("iv".equals(type)){    //ImageView 加载图片
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                int imgSrc = (int)obj[2];   //获取图片资源id
                ImageView iv = new ImageView(context);
                iv.setImageResource(imgSrc);    //设置图片资源
                iv.setScaleType(ImageView.ScaleType.FIT_XY);   //拉伸图片到给定宽高
                v.addView(iv, params);

                boolean isCanClick = (boolean)obj[3];

                //判断图片是否可点击，可点击才设置点击和触摸事件，并发送串口指令
                if(isCanClick){

                    //图片点击事件
                    iv.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            int imgId = (int)obj[2];   //获取图片资源id
                            byte[] bytes = (byte[])obj[4];   //获取该图片对应串口指令

                            Log.e("fragment_First===iv", imgId+"==="+bytes.length+"==="+bytes);

                            if(imgId==R.drawable.light){
                                iv.setImageResource(R.drawable.light_0);
                                obj[2] = R.drawable.light_0;
                            }else if(imgId==R.drawable.light_0){
                                iv.setImageResource(R.drawable.light);
                                obj[2] = R.drawable.light;
                            }else if(imgId==R.drawable.light_fw){
                                iv.setImageResource(R.drawable.light_fw_0);
                                obj[2] = R.drawable.light_fw_0;
                            }else if(imgId==R.drawable.light_fw_0){
                                iv.setImageResource(R.drawable.light_fw);
                                obj[2] = R.drawable.light_fw;
                            }

//                            write(bytes);   //TODO  发送串口指令
//                            boolean isSendy = uartJnit.getInstance().sendCmds(cmd); //TODO
                        }
                    });
                }

            }else if("mcv".equals(type)){    //MiClockView 加载小米时钟，用于屏保
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                int bgColor = (int)obj[4];  //屏保时钟界面背景

                miClockView = new MiClockView(context, (int)obj[5], (int)obj[6], (int)obj[7], (int)obj[8], (int)obj[9]);
                miClockView.setBackgroundColor(bgColor);

                v.addView(miClockView, params);

                miClockView.setVisibility(View.GONE);

                //屏保界面触摸事件，触摸界面就能关闭屏保界面，回到之前界面
                miClockView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        Log.e("mcv===onTouch", "===");

                        miClockView.setVisibility(View.GONE);
                        return false;
                    }
                });


            }else if("lrsv".equals(type)){  //加载导航转盘，用于切换显示区
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                String[] names = (String[])obj[2];   //导航转盘的每个item名称
                int textSize = (int)obj[3];   //导航转盘的每个item文字大小
                int textColor = (int)obj[4];   //导航转盘的每个item文字颜色

                LoopRotarySwitchView mLoopRotarySwitchView = new LoopRotarySwitchView(this);
                mLoopRotarySwitchView.setGravity(Gravity.CENTER);   //导航转盘里的内容居中

                //加载导航转盘的所有item
                for (int j = 0; j < names.length; j++) {
                    TextView tv=new TextView(this);
                    tv.setText(names[j]);
                    tv.setTextSize(textSize);
                    tv.setTextColor(textColor);
                    mLoopRotarySwitchView.addView(tv);
                }

                v.addView(mLoopRotarySwitchView, params);

                mLoopRotarySwitchView
                        .setR(270)  //设置导航转盘半径的大小
//                      .setAutoRotation(false)//是否自动切换
//                      .setAutoScrollDirection(LoopRotarySwitchView.AutoScrollDirection.left)//切换方向
//                      .setAutoRotationTime(2000)//自动切换的时间  单位毫秒
                        .setLoopRotationX(7);   //设置导航转盘X方向倾斜度 7度

                //导航栏item点击事件
                mLoopRotarySwitchView.setOnItemSelectedListener(new OnItemSelectedListener() {
                    @Override
                    public void selected(int position, View view) {

                        curPos = position;

                        Log.e("selected===", isEnd+"==="+pos+"==="+position+"==="+toPos+"==="+dx);

                        if(dx!=0){
                            long time = 0;

                            if(dx<0.1 && dx>-0.1){
                                Log.e("selected>>>1", isEnd+"==="+pos+"==="+position+"==="+dx+"==="+time);

                                framelayout.setAlpha(1);
                                framelayout2.setAlpha(1);

                                isStop = true;

                                FragmentManager manager = getSupportFragmentManager();
                                manager.popBackStackImmediate(Fragment_First.TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                                manager.popBackStackImmediate(Fragment_Second.TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                                FragmentTransaction tration = manager.beginTransaction();
                                tration.addToBackStack(Fragment_First.TAG);
                                tration.addToBackStack(Fragment_Second.TAG);



                                for (int i=0; i<frags.length; i++){
                                    if(position==i){
                                        tration.replace(id2, frags[i], Fragment_Second.TAG);
                                        toFrag = frags[i];
                                        toPos = i;
                                    }
                                }

                                tration.commit();

                                pos = position;
                                dx = 0;
                                first = true;
                                isEnd = false;
                            }else{
                                if(dx>=0.1){
                                    time = (long)(1000*(1-dx));
                                }else{
                                    time = (long)(1000*(1+dx));
                                }
                            }

                            //纠正最终定，智能判断要不要纠正
                            handler.postDelayed(new Runnable(){
                                public void run() {

                                    Log.e("selected===1", isEnd+"==="+pos+"==="+position+"==="+dx+"==="+(toFrag instanceof Fragment_First)+"==="+(toFrag instanceof Fragment_Second)+"==="+(toFrag instanceof Fragment_Third));


                                    if(position==toPos){

                                        Log.e("selected===3", isEnd+"==="+pos+"==="+position+"==="+dx+"==="+(fromFrag instanceof Fragment_First)+"==="+(fromFrag instanceof Fragment_Second)+"==="+(fromFrag instanceof Fragment_Third));

                                    }else{
                                        Log.e("selected===4", isEnd+"==="+pos+"==="+position+"==="+dx+"==="+(fromFrag instanceof Fragment_First)+"==="+(fromFrag instanceof Fragment_Second)+"==="+(fromFrag instanceof Fragment_Third));


                                        isStop = true;

                                        FragmentManager manager = getSupportFragmentManager();
                                        manager.popBackStackImmediate(Fragment_First.TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                                        manager.popBackStackImmediate(Fragment_Second.TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                                        FragmentTransaction tration = manager.beginTransaction();
                                        tration.addToBackStack(Fragment_First.TAG);
                                        tration.addToBackStack(Fragment_Second.TAG);

                                        for (int i=0; i<frags.length; i++){
                                            if(position==i){
                                                tration.replace(id2, frags[i], Fragment_Second.TAG);
                                                toFrag = frags[i];
                                                toPos = i;
                                            }
                                        }

                                        framelayout.setAlpha(0);
                                        framelayout2.setAlpha(1);

                                        tration.commit();

                                    }

                                    pos = position;
                                    dx = 0;
                                    first = true;
                                    isEnd = false;
                                }
                            }, time);


                        }else{

                            Log.e("selected===2", isEnd+"==="+pos+"==="+position+"==="+dx+"==="+toPos);

                            isEnd = true;

                            if(pos==0 && position==0){
                                FragmentManager manager = getSupportFragmentManager();
                                manager.popBackStackImmediate(Fragment_First.TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                                manager.popBackStackImmediate(Fragment_Second.TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                                FragmentTransaction tration = manager.beginTransaction();
                                tration.addToBackStack(Fragment_First.TAG);
                                tration.addToBackStack(Fragment_Second.TAG);

                                Log.e("selected===3", isEnd+"==="+pos+"==="+position+"==="+dx+"==="+toPos+"==="+id2+"==="+frags[0]);

                                tration.replace(id2, frags[0], Fragment_Second.TAG);
                                toFrag = frags[0];
                                toPos = 0;

                                framelayout.setAlpha(0);
                                framelayout2.setAlpha(1);

                                tration.commit();
                            }else{
                                for (int i=0; i<frags.length; i++){
                                    if(pos==i && position==(i+1)%frags.length){
                                        applyRotation(true, frags[i], frags[(i+1)%frags.length], pos, position);
                                    }else if(pos==i && position==(i-1+frags.length)%frags.length){
                                        applyRotation(false, frags[i], frags[(i-1+frags.length)%frags.length], pos, position);
                                    }
                                }
                            }

                            pos = position;

                            first = true;
                            isEnd = false;
                        }

                    }
                });

                //导航栏触摸事件
                mLoopRotarySwitchView.setOnLoopViewTouchListener(new OnLoopViewTouchListener() {
                    @Override
                    public void onTouch(MotionEvent event) {

                        if(event.getAction()==0){
                            isStop = false;
                        }

                        Log.e("onTouch===0", first+"==="+isEnd+"==="+dx+"==="+x+"==="+event.getX()+"==="+event.getY()+"==="+event.getAction());

                        if(first){


                            x = event.getX();
                            first = false;

                        }

                        if(isEnd){
                            return;
                        }

                        dx = (x-event.getX())/200;



                        Log.e("onTouch===", pos+"==="+dx+"==="+x+"==="+event.getX()+"==="+event.getY()+"==="+event);


                        if(event.getAction()==1){   //手指离开屏幕
                            isEnd = true;
                            first = true;

                            left_first = true;

                        }else{
                            isEnd = false;
                        }

//                        if(dx<0.1 && dx>-0.1){
//                            return;
//                        }

                        if(dx>0.1){
                            Log.e("onTouch===1", isEnd+"==="+dx+"==="+pos);

                            for (int i=0; i<frags.length; i++){
                                if(pos==i){
                                    applyRotation(true, frags[i], frags[(i+1)%frags.length], i, (i+1)%frags.length);
                                }
                            }

                            degrees = Util_Rotate3DAnimation2.degrees;
                            degrees2 = Util_Rotate3DAnimation2.degrees2;
                        }else if(dx<-0.1){
                            Log.e("onTouch===2", isEnd+"==="+dx+"==="+pos);

                            for (int i=0; i<frags.length; i++){
                                if(pos==i){
                                    applyRotation(false, frags[i], frags[(i-1+frags.length)%frags.length], i, (i-1+frags.length)%frags.length);
                                }
                            }

                            degrees = Util_Rotate3DAnimation2.degrees;
                            degrees2 = Util_Rotate3DAnimation2.degrees2;
                        }



                        Log.e("onTouch===3", isEnd+"==="+dx+"==="+degrees+"==="+degrees2+"==="+mLoopRotarySwitchView.getSelectItem()+"==="+event.getX()+"==="+event.getY()+"==="+event.getAction());


                    }
                });

            }else if("fl".equals(type)){    //显示区
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                framelayout = new FrameLayout(this);
                framelayout.setId(id);  //给每个framelayout设置ID，用于旋转逻辑
                v.addView(framelayout, params);

                framelayout2 = new FrameLayout(this);
                framelayout2.setId(id2);
                v.addView(framelayout2, params);

            }
        }

    }

    //轮循更新温度、时间、日期、屏保时钟
    private void loop(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                long n=0;

                while (true){

                    m_myHandler.sendEmptyMessage(1);    //更新温度、时间、日期

                    try {
                        Thread.sleep(loopTime);     //轮循时间
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    n++;

                    if(n%cn==0){
                        m_myHandler.sendEmptyMessage(2);    //显示屏保时钟
                    }

                }

            }
        }).start();
    }

    //轮循所用Handler，android传递消息的机制
    protected Handler m_myHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message mes) {
            switch (mes.what) {
                case 0:
                    break;

                case 1:
                    //获取当前时间
                    Date date = new Date(System.currentTimeMillis());
                    String time1 = simpleDateFormat.format(date);
                    String time2 = simpleDateFormat2.format(date);

                    Log.e("m_myHandler===1", "==="+bytes_temp);

                    //串口读取车内温度
//                    read(bytes_temp);     //TODO
                    int read_count = uartJni.read(fd, read_buf, read_buf.length);   //TODO

                    tv_temp.setText("25℃");
                    tv_time.setText(time1);
                    tv_date.setText(time2);

                    break;

                case 2:
                    miClockView.setVisibility(View.VISIBLE);
                    break;

                default:
                    break;
            }
            return false;
        }
    });

    //显示区3D旋转切换，当前界面和目的界面能同时显示
    public void applyRotation(final boolean zheng, Fragment fromFragment, Fragment toFragment, int fromPosition, int toPosition) {
        fromFrag = fromFragment;
        toFrag = toFragment;
        toPos = toPosition;

        float centerX;
        float centerY;
        float start;
        float end;

        if(dx==0){
            if(zheng){
                FragmentManager manager = getSupportFragmentManager();
                manager.popBackStackImmediate(Fragment_First.TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                manager.popBackStackImmediate(Fragment_Second.TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                FragmentTransaction tration = manager.beginTransaction();
                tration.addToBackStack(Fragment_First.TAG);
                tration.addToBackStack(Fragment_Second.TAG);
                tration.replace(id, fromFragment, Fragment_First.TAG).replace(id2, toFragment, Fragment_Second.TAG).commit();

                if(toFragment instanceof Fragment_First){
                    Log.e("tration===0", tration.isEmpty()+"===");
                }else{

                }

                //当前页逐渐关闭
                centerX = width;
                centerY = 0;
                start = 0;
                end = -90;

                Log.e("applyRotation===2", "==="+centerX+"==="+centerY+"==="+framelayout.getWidth()+"==="+framelayout.getHeight());

                // Create a new 3D rotation with the supplied parameter
                // The animation listener is used to trigger the next animation
                Util_Rotate3DAnimation2 rotation2 = new Util_Rotate3DAnimation2(framelayout, width, dx, start, end, centerX, centerY, 0.0f, zheng, false, isEnd);
                rotation2.setDuration(1000);
                rotation2.setFillAfter(true);
                rotation2.setInterpolator(new AccelerateInterpolator());
                framelayout.startAnimation(rotation2);


                //下一页逐渐显示
                centerX = 0;
                centerY = 0;
                start = 30;
                end = 0;

                Log.e("applyRotation===", isEnd+"==="+dx+"==="+degrees+"==="+centerX+"==="+centerY+"==="+framelayout.getWidth()+"==="+framelayout.getHeight());

                Util_Rotate3DAnimation2 rotation = new Util_Rotate3DAnimation2(framelayout2, width, dx, start, end, centerX, centerY, 0.0f, zheng, true, isEnd);
                rotation.setDuration(1000);
                rotation.setFillAfter(true);
                rotation.setInterpolator(new AccelerateInterpolator());
                framelayout2.startAnimation(rotation);

                Log.e("applyRotation===1", isEnd+"==="+dx+"==="+degrees2+"==="+centerX+"==="+centerY+"==="+framelayout.getWidth()+"==="+framelayout.getHeight());


            }else{
                //当前页逐渐关闭
                FragmentManager manager = getSupportFragmentManager();
                manager.popBackStackImmediate(Fragment_First.TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                manager.popBackStackImmediate(Fragment_Second.TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                FragmentTransaction tration = manager.beginTransaction();
                tration.addToBackStack(Fragment_First.TAG);
                tration.addToBackStack(Fragment_Second.TAG);
                tration.replace(id, fromFragment, Fragment_First.TAG).replace(id2, toFragment, Fragment_Second.TAG).commit();

                Log.e("tration===", tration.isEmpty()+"===");

                centerX = 0;
                centerY = 0;
                start = 0;
                end = 90;

                Log.e("applyRotation===", isEnd+"==="+dx+"==="+degrees+"==="+centerX+"==="+centerY+"==="+framelayout.getWidth()+"==="+framelayout.getHeight());

                Util_Rotate3DAnimation2 rotation = new Util_Rotate3DAnimation2(framelayout, width, dx, start, end, centerX, centerY, 0.0f, zheng, true, isEnd);
                rotation.setDuration(1000);
                rotation.setFillAfter(true);
                rotation.setInterpolator(new AccelerateInterpolator());
                framelayout.startAnimation(rotation);


                Log.e("applyRotation===1", isEnd+"==="+dx+"==="+degrees2+"==="+centerX+"==="+centerY+"==="+framelayout2.getWidth()+"==="+framelayout2.getHeight());

                //下一页逐渐显示
                centerX = width;
                centerY = 0;
                start = -30;
                end = 0;

                Util_Rotate3DAnimation2 rotation2 = new Util_Rotate3DAnimation2(framelayout2, width, dx, start, end, centerX, centerY, 0.0f, zheng, false, isEnd);
                rotation2.setDuration(1000);
                rotation2.setFillAfter(true);
                rotation2.setInterpolator(new AccelerateInterpolator());
                framelayout2.startAnimation(rotation2);
            }

        }else if(dx>0){       //右转
            Log.e("right===", left_first+"==="+isEnd);

            if((left_first && !isEnd) || toPos!=toPos2){
                left_first = false;
                toPos2 = toPos;

                FragmentManager manager = getSupportFragmentManager();
                manager.popBackStackImmediate(Fragment_First.TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                manager.popBackStackImmediate(Fragment_Second.TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                FragmentTransaction tration = manager.beginTransaction();
                tration.addToBackStack(Fragment_First.TAG);
                tration.addToBackStack(Fragment_Second.TAG);
                tration.replace(id, fromFragment, Fragment_First.TAG).replace(id2, toFragment, Fragment_Second.TAG).commit();

                if(toFragment instanceof Fragment_First){
                    Log.e("tration===0", tration.isEmpty()+"===");
                }else if(toFragment instanceof Fragment_Second){
                    Log.e("tration===1", tration.isEmpty()+"===");
                }else if(toFragment instanceof Fragment_Third){
                    Log.e("tration===2", tration.isEmpty()+"===");
                }
            }

            //当前页逐渐关闭
            centerX = width;
            centerY = 0;
            start = isEnd?degrees2:0;
            end = -90;
            dx = dx>1?1:dx;

            Log.e("applyRotation===", "==="+centerX+"==="+centerY+"==="+framelayout.getWidth()+"==="+framelayout.getHeight());

            Util_Rotate3DAnimation2 rotation2 = new Util_Rotate3DAnimation2(framelayout, width, dx, start, end, centerX, centerY, 0.0f, zheng, false, isEnd);
            rotation2.setDuration((long)(1000*(1-dx)));
            rotation2.setFillAfter(true);
            rotation2.setInterpolator(new AccelerateInterpolator());
            framelayout.startAnimation(rotation2);


            Log.e("applyRotation===1", isEnd+"==="+dx+"==="+degrees2);

            //下一页逐渐显示
            centerX = 0;
            centerY = 0;
            start = isEnd?degrees:30;
            end = 0;

            Log.e("applyRotation===2", isEnd+"==="+dx+"==="+degrees);

            // Create a new 3D rotation with the supplied parameter
            // The animation listener is used to trigger the next animation
            Util_Rotate3DAnimation2 rotation = new Util_Rotate3DAnimation2(framelayout2, width, dx, start, end, centerX, centerY, 0.0f, zheng, true, isEnd);
            rotation.setDuration((long)(1000*(1-dx)));
            rotation.setFillAfter(true);
            rotation.setInterpolator(new AccelerateInterpolator());
            framelayout2.startAnimation(rotation);

        }else if(dx<0){  //左转
            //当前页逐渐关闭
            Log.e("left===", left_first+"==="+isEnd);

            if((left_first && !isEnd) || toPos!=toPos2){
                left_first = false;
                toPos2 = toPos;

                FragmentManager manager = getSupportFragmentManager();
                manager.popBackStackImmediate(Fragment_First.TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                manager.popBackStackImmediate(Fragment_Second.TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                FragmentTransaction tration = manager.beginTransaction();
                tration.addToBackStack(Fragment_First.TAG);
                tration.addToBackStack(Fragment_Second.TAG);
                tration.replace(id, fromFragment, Fragment_First.TAG).replace(id2, toFragment, Fragment_Second.TAG).commit();

                Log.e("tration===", tration.isEmpty()+"===");
            }

            centerX = 0;
            centerY = 0;
            start = isEnd?-degrees:0;;
            end = 90;

            dx = dx<-1?-1:dx;

            Log.e("applyRotation===", isEnd+"==="+dx+"==="+degrees+"==="+centerX+"==="+centerY+"==="+framelayout.getWidth()+"==="+framelayout.getHeight());

            Util_Rotate3DAnimation2 rotation = new Util_Rotate3DAnimation2(framelayout, width, dx, start, end, centerX, centerY, 0.0f, zheng, true, isEnd);
            rotation.setDuration((long)(1000*(1+dx)));
            rotation.setFillAfter(true);
            rotation.setInterpolator(new AccelerateInterpolator());
            framelayout.startAnimation(rotation);


            Log.e("applyRotation===1", isEnd+"==="+dx+"==="+degrees2+"==="+centerX+"==="+centerY+"==="+framelayout2.getWidth()+"==="+framelayout2.getHeight());

            //下一页逐渐显示
            centerX = width;
            centerY = 0;
            start = isEnd?degrees2:-30;
            end = 0;

            Util_Rotate3DAnimation2 rotation2 = new Util_Rotate3DAnimation2(framelayout2, width, dx, start, end, centerX, centerY, 0.0f, zheng, false, isEnd);
            rotation2.setDuration((long)(1000*(1+dx)));
            rotation2.setFillAfter(true);
            rotation2.setInterpolator(new AccelerateInterpolator());
            framelayout2.startAnimation(rotation2);
        }

    }


}
